package com.manor.paradise.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.KLog
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.EmploymentRecordsAdapter
import com.manor.paradise.bean.EmploymentBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.fragment_rule_description.*

/**
 * 雇佣记录
 */
class EmploymentRecordsFragment : Fragment(){
    var mCurView: View? = null
    internal var pageNum = 1
    internal lateinit var mAdapter: EmploymentRecordsAdapter

    companion object{
        fun newInstance(): EmploymentRecordsFragment {
            return EmploymentRecordsFragment()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        mCurView = inflater.inflate(R.layout.fragment_rule_description, container, false)
        return mCurView
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        rcv_list.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        mAdapter = EmploymentRecordsAdapter(R.layout.item_employment,null)
        rcv_list.adapter = mAdapter

        KLog.e("tag", "////////////// onViewCreated is not null ${mCurView != null}")
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData()
            }
        })
    }
    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if(isVisibleToUser){
            setData()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        KLog.e("tag", "////////////// onCreate is not null ${mCurView != null}")
    }
    fun setData() {
        ElvesHttpUtil.getMyhirelist(pageNum, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var shopListBean: EmploymentBean = GsonUtil.gsonToBean<EmploymentBean>(info, EmploymentBean::class.java)
                KLog.e("json", shopListBean)
                if(pageNum==1){
                    mAdapter.setNewInstance(shopListBean.records)
                    refreshLayout?.finishRefresh()
                if(shopListBean.records.size==0){
                    mEmptyView?.showNull()
                }else{
                    mEmptyView?.visibility = View.GONE
                }
                }else{
                    mAdapter.addData(shopListBean.records)
                    refreshLayout?.finishLoadMore()
                }

            }

        })
    }
    override fun onDestroy() {
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_HIRE_LIST)
        super.onDestroy()
    }
}